package com.codedancer.campus.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.core.BaseMapper;
import com.codedancer.campus.po.CircleFollow;
import com.codedancer.campus.qo.circle.CircleFollowPageSearchQo;
import com.codedancer.campus.qo.circle.CirclePageSearchQo;

public interface CircleFollowMapper extends BaseMapper<CircleFollow> {

    /**
     * 当前用户是否关注
     *
     * @param uuid
     * @param currentUserUuid
     * @return
     */
    default Boolean isFollow(String uuid, String currentUserUuid) {
        LambdaQueryWrapper<CircleFollow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CircleFollow::getCircleUuid, uuid);
        lambdaQueryWrapper.eq(CircleFollow::getFollowUserUuid, currentUserUuid);
        return selectCount(lambdaQueryWrapper) > 0;
    }

    /**
     * 取消关注
     *
     * @param uuid
     * @param currentLoginUuid
     */
    default void cancelFollow(String uuid, String currentLoginUuid) {
        LambdaQueryWrapper<CircleFollow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CircleFollow::getCircleUuid, uuid);
        lambdaQueryWrapper.eq(CircleFollow::getFollowUserUuid, currentLoginUuid);
        delete(lambdaQueryWrapper);
    }

    /**
     * 分页查询某个圈子关注用户列表
     *
     * @param circleFollowPage
     * @param circleFollowPageSearchQo
     * @return
     */
    default IPage<CircleFollow> page(Page<CircleFollow> circleFollowPage,
        CircleFollowPageSearchQo circleFollowPageSearchQo) {
        LambdaQueryWrapper<CircleFollow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
            .eq(CircleFollow::getCircleUuid, circleFollowPageSearchQo.getCircleUuid());
        return selectPage(circleFollowPage, lambdaQueryWrapper);
    }

    /**
     * 查询用户关注的圈子
     *
     * @param circleFollowPage
     * @param circlePageSearchQo
     * @return
     */
    default IPage<CircleFollow> pageFollow(Page<CircleFollow> circleFollowPage,
        CirclePageSearchQo circlePageSearchQo) {
        LambdaQueryWrapper<CircleFollow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
            .eq(CircleFollow::getFollowUserUuid, circlePageSearchQo.getCurrentUserUuid());
        lambdaQueryWrapper.orderByDesc(CircleFollow::getCreateTime);
        return selectPage(circleFollowPage, lambdaQueryWrapper);
    }

}
